package com.ruoyi.web.controller.app.service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.base.XException;
import com.ruoyi.framework.web.exception.base.XResult;
import com.ruoyi.sj.domain.Dynamic;
import com.ruoyi.sj.domain.vo.DynamicDetails;
import com.ruoyi.sj.mapper.CommentsMapper;
import com.ruoyi.sj.mapper.DynamicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MyDynamicService
 * @Description TODO
 * @Author tyc@yanyu
 * @Date 2018/12/6 0006 17:36
 **/
@Service
public class AppDynamicService {
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private CommentsMapper commentsMapper;

    /**
     * 发布朋友圈
     * @param map
     * @return
     */
    public Object addDynamic(Map<String, Object> map) {
        Dynamic dynamic =new Dynamic();
        dynamic.setContent(map.get("content")+""); //内容
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        dynamic.setCreateDate(df.format(new Date())); //发布时间
        dynamic.setPicture(map.get("picture")+"");  //图片或者视频
        dynamic.setMemberId(Long.valueOf(map.get("memberId")+"")); //用户ID号
        dynamicMapper.insertDynamic(dynamic);
        return dynamic.getId();
    }

    /**
     * 查询用户动态列表
     * @param memberId
     * @return
     */
    public List<DynamicDetails> queryByMemberId(String memberId) {
        if (StringUtils.isBlank(memberId)){
            throw new XException("用户ID不能为空");
        }
        List<DynamicDetails> list = dynamicMapper.queryByMemberId(Long.valueOf(memberId));
        return list;
    }

    /**
     * 删除动态，相应的评论也删除
     * @param dynamicId
     * @return
     */
    @Transactional
    public Object deleteDynamic(String dynamicId) {
        //删除动态
        int dy = dynamicMapper.deleteDynamicById(Long.valueOf(dynamicId));
        //删除动态下评论
        commentsMapper.deleteCommentsByDynamicId(dynamicId);
        return dy;
    }

    /**
     * 查询所有朋友动态
     * @param memberId
     * @return
     */
    public List<Map<String,Object>> getDynamicList(String memberId) {
        if (StringUtils.isBlank(memberId)){
            throw new XException("用户ID不能为空");
        }
        return dynamicMapper.getFriends(memberId);
    }

    public List<DynamicDetails> queryAlbumList(String memberId) {
        return dynamicMapper.queryAlbumList(memberId);
    }
}
